package coms.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import coms.datatransfer.ConferenceDTO;
import coms.datatransfer.ConferenceItemDTO;
//import coms.datatransfer.ConferenceItem_User;
import coms.ejb.sessionbean.ConferenceBeanRemote;
import coms.ejb.sessionbean.ConferenceItemBeanRemote;
import coms.ejb.sessionbean.PaymentBeanRemote;
//import coms.ejb.sessionbean.PaymentBeanRemote;
import coms.service.BusinessDelegate;

import java.util.*;

public class ConferenceItemAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private ConferenceDTO conference = new ConferenceDTO();	
	private ConferenceItemDTO conferenceItem = new ConferenceItemDTO();
	private List<ConferenceItemDTO> conferenceItemList = new ArrayList<ConferenceItemDTO>();
	private List<ConferenceItemDTO> conferenceItemListofUser = new ArrayList<ConferenceItemDTO>();
	private int userregisteredornot;
	private BusinessDelegate delegate;
	private ConferenceBeanRemote conferenceBean;
	private PaymentBeanRemote paymentBean;
	private ConferenceItemBeanRemote conferenceItemBean;
	
	public ConferenceItemAction()
	{
		this.delegate = new BusinessDelegate();
		conferenceBean = (ConferenceBeanRemote)delegate.getBean("conferenceBean");
		paymentBean=(PaymentBeanRemote)delegate.getBean("PaymentBean");
		conferenceItemBean = (ConferenceItemBeanRemote)delegate.getBean("conferenceItemBean");
	}
	
	public int getUserregisteredornot() {return userregisteredornot;}
	public void setUserregisteredornot(int userregisteredornot) {this.userregisteredornot = userregisteredornot;}

	public List<ConferenceItemDTO> getConferenceItemListofUser() {return conferenceItemListofUser;}
	public void setConferenceItemListofUser(List<ConferenceItemDTO> conferenceItemListofUser) {this.conferenceItemListofUser = conferenceItemListofUser;}

	public ConferenceItemDTO getConferenceItem(){return this.conferenceItem;}
	public void setConferenceItem(ConferenceItemDTO conferenceItem){this.conferenceItem = conferenceItem;}	
	
	public String execute() throws Exception {		
		return "success";
	}	
	
	public String saveOrUpdateItem() {		
		//conferenceItemBean.createConferenceItem(conferenceItem);
		try{
		Map<String,Object> session = ActionContext.getContext().getSession();
		if(session.get("conferenceId") != null){
			conference = conferenceBean.findConference(Integer.parseInt(session.get("conferenceId").toString()));
		}else if(session.get("createConfId") != null){
			conference = conferenceBean.findConference(Integer.parseInt(session.get("createConfId").toString()));
		}
		setMaxReviewersOfItem(conferenceItem);
		conference.getListOfConferenceItems().add(conferenceItem);			
		conferenceBean.createConference(conference);
		return "success";
		} catch (Exception e){
			return "exception";
		}
		
	}
	
	private void setMaxReviewersOfItem(ConferenceItemDTO conferenceItem){
		if (conferenceItem.getConferenceItemType() == 1){
			conferenceItem.setMaxReviewers(5);
		}else if (conferenceItem.getConferenceItemType() == 2){
			conferenceItem.setMaxReviewers(2);
		}else if (conferenceItem.getConferenceItemType() == 3){
			conferenceItem.setMaxReviewers(3);
		}
	}
	
	public String displayConferenceItemsList() throws Exception {
		
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		ConferenceItemBeanRemote bean = (ConferenceItemBeanRemote)delegate.getBean("conferenceItemBean");
		
		if(request.getParameter("conferenceID") != null)
		session.put("conferenceId", request.getParameter("conferenceID"));
		
		if(session.get("conferenceId") != null)
		conferenceItemList = (List<ConferenceItemDTO>)bean.findAllConferenceItemByConfId(Integer.parseInt((String)session.get("conferenceId")));
		return "display";
	}
	
     
	public void createFileItem() throws Exception {
		
	}
	
	public List<ConferenceItemDTO> getConferenceItemList() {
		return conferenceItemList;
	}

	public void setConferenceItemList(List<ConferenceItemDTO> conferenceItemList) {
		this.conferenceItemList = conferenceItemList;
	}

	/*public ConferenceItemDTO getConferenceItem() {
		return conferenceItem;
	}

	public void setConferenceItem(ConferenceItemDTO conferenceItem) {
		this.conferenceItem = conferenceItem;
	}*/
	
	
	
	
	public String displayConferenceItemsofConference() throws NumberFormatException, Exception{
		
		
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		ConferenceItemBeanRemote bean = (ConferenceItemBeanRemote)delegate.getBean("conferenceItemBean");
		
		
		if(request.getParameter("conferenceID") != null)
			session.put("conferenceId", request.getParameter("conferenceID"));
		
		
		if(session.get("conferenceId") != null)
			conferenceItemList = (ArrayList<ConferenceItemDTO>)bean.          
				findAllConferenceItemByConfId(Integer.parseInt((String)session.get("conferenceId")));
		
		
		//System.out.println("User ID:"+session.get("userID"));
		
		
		if(session.get("userID")!= null){
			
		
		
				conferenceItemListofUser=(ArrayList<ConferenceItemDTO>)
							conferenceBean.findRegisteredConferenceItems(Integer.parseInt(session.get("userID").toString()));
		}
		
		System.out.println("conferenceItemListofUser:" +conferenceItemListofUser.size());
		
		
		
		
			
		return "SUCCESS";
	}
	
	
	
	//public String displayBookedConferenceItemsofConference() throws NumberFormatException, Exception{
public String displayBookedConferenceItemsofConference() throws NumberFormatException, Exception{
	
	
	paymentBean.initialize();
	
	Map<String,Object> session = ActionContext.getContext().getSession();
	
		System.out.println(" I am displayBookedConferenceItemsofConference");
		
		System.out.println(" I am checking session user id:"+Integer.parseInt(session.get("userID").toString()));
		
		
		//HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		
		
	
		conferenceItemList = (ArrayList<ConferenceItemDTO>)
				conferenceBean.findRegisteredConferenceItems(Integer.parseInt(session.get("userID").toString()));
	
		for(ConferenceItemDTO a :conferenceItemList){
			
			if(paymentBean.getAllTransactions(a.getConferenceItemID(), Integer.parseInt(session.get("userID").toString())).size()>0){
				
				
				a.setPaymentStatus("Succesfull");
			}
				
			else
					a.setPaymentStatus("Make Payment");
			
			a.getPaymentStatus().endsWith("Succesfull");
				
			}
			
		
			
		
	
			System.out.println("User ID:"+session.get("userID"));
			
			System.out.println("SIZE:"+conferenceItemList.size());
		return "SUCCESS";
	}
	
	/*public void validate(){
		if(conferenceItem.getDescription().length() == 0){
			addFieldError("conferenceItem.description","Description is required");
		}
		if(conferenceItem.getDeadlineForSubmission().toString().length() == 0){
			addFieldError("conferenceItem.deadlineForSubmission","Deadline is required");
		}
		if(conferenceItem.getStartTime().toString().length() == 0){
			addFieldError("conferenceItem.startTime","Start time is required");
		}
		if(conferenceItem.getEndTime().toString().length() == 0){
			addFieldError("conferenceItem.endTime","End time is required");
		}
		if(Integer.toString(conferenceItem.getMaximumNumberOfParticipants()).length() == 0){
			addFieldError("conferenceItem.maximumNumberOfParticipants","Maximum number of participants is required");
		}
		if(Integer.toString(conferenceItem.getMaximumNumberOfSubmissions()).length() == 0){
			addFieldError("conferenceItem.maximumNumberOfSubmissions","Maximum number of submissions is required");
		}
		if(Integer.toString(conferenceItem.getItemFee()).length() == 0){
			addFieldError("conferenceItem.itemFee","Item fee is required");
		}
	}*/
}
